package com.atguigu.gulimall.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @author : xyq
 * @Description: 自定义SpringSession完成子域session共享
 * @date: 2020/7/28/18:47
 */
@Configuration
public class GulimallSessionConfig {
    /**
     * 使用CookieSerializer负责定义会话cookie的编写方式，来实现子域session共享
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setDomainName("gulimall.com");  // 指定作用域为整个父类作用域名
        serializer.setCookieName("GULISESSION");
        return serializer;
    }

    /**
     * 容器中放入redis序列化器
     * 使用JSON的序列化方式来序列化对象到Redis中
     */
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }

}
